package com.dlee.mvc.parse;

import java.io.File;
import java.lang.annotation.Annotation;

import com.dlee.mvc.annotion.Aspect;
import com.dlee.mvc.annotion.Controller;
import com.dlee.mvc.annotion.Service;
import com.dlee.mvc.container.BeanContainer;
import com.dlee.mvc.util.FileUtil;

/**
 * 解析类上注解
 * @author Administrator
 *
 */
public class ClassAnnotationParse implements Parse {

	@Override
	public void parse(File classFile, String contextPath) throws ClassNotFoundException {
		// 抽取类的路径，再通过反射取类的实例
        String classFullPath = FileUtil.getClassFullName (classFile.getPath (), BeanContainer.contextEnvironment.getAnnotationScanPath ());
        Class objClass = Class.forName (classFullPath);
        // 取类的注解类
        Annotation[] classAnnotation = objClass.getAnnotations ();
        //Annotation[] classAnnotation = ClassUtils.getAnnotation (classFile, BeanContainer.contextEnvironment.getAnnotationScanPath ());
        // 当类上存在注解
        if (classAnnotation.length != 0)
        {
            // 先初始化类
            for (Annotation annotation : classAnnotation)
            {
                Object obj = annotation.annotationType ();
                // 如果和已定义的注解是同一个，则可以进行注入
                if (obj.equals (Controller.class) || obj.equals (Service.class))
                {
                	BeanContainer.newInstanceClass (objClass);
                }
                // 解析@Aspect注解，做切面
                else if (obj.equals (Aspect.class))
                {
                    Aspect aspect = (Aspect) annotation;
                    BeanContainer.newInstanceClass (objClass);
                    BeanContainer.initAspect (objClass, aspect);
                }
            }
        }

	}

}
